home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / pgp23src.zip / SRC / MDFILE.C < prev    next >
C/C++ Source or Header  |  1993-05-09  |  3KB  |  103 lines

  1. /*    mdfile.c  - Message Digest routines for PGP.
  2.     PGP: Pretty Good(tm) Privacy - public key cryptography for the masses.
  3.  
  4.     (c) Copyright 1990-1992 by Philip Zimmermann.  All rights reserved.
  5.     The author assumes no liability for damages resulting from the use
  6.     of this software, even if the damage results from defects in this
  7.     software.  No warranty is expressed or implied.
  8.  
  9.     All the source code Philip Zimmermann wrote for PGP is available for
  10.     free under the "Copyleft" General Public License from the Free
  11.     Software Foundation.  A copy of that license agreement is included in
  12.     the source release package of PGP.  Code developed by others for PGP
  13.     is also freely available.  Other code that has been incorporated into
  14.     PGP from other sources was either originally published in the public
  15.     domain or was used with permission from the various authors.  See the
  16.     PGP User's Guide for more complete information about licensing,
  17.     patent restrictions on certain algorithms, trademarks, copyrights,
  18.     and export controls.  
  19. */
  20.  
  21. #include <stdio.h>
  22. #include "mpilib.h"
  23. #include "mdfile.h"
  24. #include "fileio.h"
  25. #include "language.h"
  26. #include "pgp.h"
  27.  
  28. /* Begin MD5 routines */
  29.  
  30. /* Note - the routines in this module, except for MD_addbuffer,
  31.  * do not "finish" the MD5 calculation.  MD_addbuffer finishes the
  32.  * calculation in each case, usually to append the timestamp and class info.
  33.  */
  34.  
  35. /* Computes the message digest for a file from current position for
  36.    longcount bytes.
  37.    Uses the RSA Data Security Inc. MD5 Message Digest Algorithm */
  38. int MDfile0_len(MD5_CTX *mdContext, FILE *f, word32 longcount)
  39. {    int bytecount;
  40.     unsigned char buffer[1024];
  41.  
  42.     MD5Init(mdContext);
  43.     /* Process 1024 bytes at a time... */
  44.     do
  45.     {
  46.         if (longcount < (word32) 1024)
  47.             bytecount = (int)longcount;
  48.         else
  49.             bytecount = 1024;
  50.         bytecount = fread(buffer, 1, bytecount, f);
  51.         if (bytecount>0)
  52.         {    MD5Update(mdContext, buffer, bytecount);
  53.             longcount -= bytecount;
  54.         }
  55.         /* if text block was short, exit loop */
  56.     } while (bytecount==1024);
  57.     return(0);
  58. }    /* MDfile0_len */
  59.  
  60.  
  61. /* Computes the message digest for a file from current position to EOF.
  62.    Uses the RSA Data Security Inc. MD5 Message Digest Algorithm */
  63.  
  64. static int MDfile0(MD5_CTX *mdContext,FILE *inFile)
  65. {    int bytes;
  66.     unsigned char buffer[1024];
  67.  
  68.     MD5Init(mdContext);
  69.     while ((bytes = fread(buffer,1,1024,inFile)) != 0)
  70.         MD5Update(mdContext,buffer,bytes);
  71.     return(0);
  72. }
  73.  
  74. /* Computes the message digest for a specified file */
  75.  
  76. int MDfile(MD5_CTX *mdContext,char *filename)
  77. {
  78.     FILE *inFile;
  79.     inFile = fopen(filename,FOPRBIN);
  80.  
  81.     if (inFile == NULL)
  82.     {    fprintf(pgpout,PSTR("\n\007Can't open file '%s'\n"),filename);
  83.         return(-1);
  84.     }
  85.     MDfile0(mdContext,inFile);
  86.     fclose (inFile);
  87.     return(0);
  88. }
  89.  
  90. /* Add a buffer's worth of data to the MD5 computation.  If a digest
  91.  * pointer is supplied, complete the computation and write the digest.
  92.  */
  93. void MD_addbuffer (MD5_CTX *mdContext, byte *buf, int buflen, byte digest[16])
  94. {
  95.     MD5Update(mdContext,buf,buflen);
  96.     if (digest) {
  97.         MD5Final(digest, mdContext);
  98.         burn(*mdContext);    /* Paranoia */
  99.     }
  100. }
  101.  
  102. /* End MD5 routines */
  103.